package com.hy.appium1;

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class AppiumDemo1MultiTouchAction {

    AndroidDriver driver;


    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.baidu.BaiduMap");
        //默认启动不初始化软件
        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.baidu.baidumaps.WelcomeScreen");
        //创建驱动
//        AndroidDriver<WebElement> Driver = new AndroidDriver<WebElement>(url, desiredCapabilities);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() {


        System.out.println("True");
        //         com.baidu.BaiduMap:id/ll_location_buttons   360   830     1150
//        driver.findElementById("com.baidu.BaiduMap:id/ll_location_buttons").click();
        //com.baidu.BaiduMap:id/zoom_out//点击缩小

        TouchAction touchAction2 = new TouchAction(driver);

        PointOption startPoint = PointOption.point(360, 830);
        PointOption endPoint = PointOption.point(360, 1150);

        touchAction2.press(startPoint).waitAction(WaitOptions.waitOptions(Duration.ofMillis(800))).moveTo(endPoint).release();

        touchAction2.perform();
        WebElement byId = driver.findElementById("com.baidu.BaiduMap:id/zoom_out");
        for (int i = 0; i <5 ; i++) {
            byId.click();
        }
        //com.baidu.BaiduMap:id/zoom_in//点击放大
        driver.findElementById("com.baidu.BaiduMap:id/zoom_in").click();
        //实现多点触摸操作需要实例化
        MultiTouchAction multiTouchAction = new MultiTouchAction(driver);

        TouchAction touchAction = new TouchAction(driver);
        TouchAction touchAction1 = new TouchAction(driver);

        //获取当前屏幕的分辨路（宽  高）

        int x = driver.manage().window().getSize().getWidth();


        int y = driver.manage().window().getSize().getHeight();

        System.out.println("高：" + x + "，宽:" + y);

        touchAction.press(PointOption.point(x * 2 / 10, y * 2 / 10))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
                .moveTo(PointOption.point(x * 4 / 10, y * 4 / 10)).release();
        touchAction1.press(PointOption.point(x * 8 / 10, y * 8 / 10))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
                .moveTo(PointOption.point(x * 6 / 10, y * 6 / 10)).release();

        multiTouchAction.add(touchAction);
        multiTouchAction.add(touchAction1);

        multiTouchAction.perform();

        System.out.println("====================================");



        touchAction.press(PointOption.point(x * 4 / 10, y * 4 / 10))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(5000)))
                .moveTo(PointOption.point(x * 2 / 10, y * 2 / 10)).release();
        touchAction1.press(PointOption.point(x * 6 / 10, y * 6 / 10))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(5000)))
                .moveTo(PointOption.point(x * 8 / 10, y * 8 / 10)).release();

        multiTouchAction.add(touchAction);
        multiTouchAction.add(touchAction1);

        multiTouchAction.perform();

    }

    @AfterMethod
    public void TestDown() {
//        Driver.quit();
    }
}
